package com.tools;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class Address {
//    static String ipAddress = "http://192.168.179.92:8888/api/v1";
    static String ipAddress = "http://124.222.25.115:8888/api/v1";

    /**
     * user
     */
    //登录
    static String userLogin = "/user/login";
    //用户信息
    static String userInfo = "/user/info";
    //注册
    static String userRegister = "/user/register";
    //获取id
    static String userLoginId = "/user/loginId";
    //退出
    static String userLogout = "/user/logout";
    static String userIsLogin = "/user/isLogin";

    /**
     * goods 普通产品
     */
    //详细信息
    static String goodsDetail = "/goods/detail";
    //列表
    static String goodsList = "/goods/list";
    //info 为了搜索
    static String goodsInfo = "/goods/info";


    /**
     * secKillGoods 秒杀产品
     */
    //详细信息
    static String secGoodsDetail = "/seckill-goods/detail";
    //列表
    static String secGoodsList = "/seckill-goods/list";
    //秒杀接口
    static String secDoSec = "/seckill/doseckill";
    //秒杀结果
    static String secResult = "/seckill/result";
    //info 为了搜索
    static String secInfo = "/seckill-goods/info";

    /**
     * 预约
     */
    static String appointment = "/appointment/create";
    static String appointList = "/appointment/list";

    /**
     * user相关api
     */
    public static String getUserLogin() {
        return ipAddress + userLogin;
    }

    public static String getUserInfo() {
        return ipAddress + userInfo;
    }

    public static String getUserRegister(){
        return ipAddress + userRegister;
    }

    public static String getUserLogout(){
        return ipAddress + userLogout;
    }

    public static String getUserIsLogin(){
        return  ipAddress + userIsLogin;
    }

    public static String getUserLoginId(){
        return ipAddress + userLoginId;
    }


    /**
     * goods相关api
     */
    public static String getGoodsDetail() {
        return ipAddress + goodsDetail;
    }

    public static String getGoodsList(){
        return ipAddress + goodsList;
    }

    public static String getGoodsInfo(){
        return ipAddress + goodsInfo;
    }


    /**
     * secKillGoods相关api
     */
    public static String getSecGoodsDetail(){
        return ipAddress + secGoodsDetail;
    }

    public static String getSecGoodsList(){
        return ipAddress + secGoodsList;
    }

    public static String getSecDoSec(){
        return ipAddress + secDoSec;
    }

    public static String getSecResult(){
        return ipAddress + secResult;
    }

    public static String getSecInfo(){
        return ipAddress + secInfo;
    }

    /**
     * 预约相关api
     */
    public static String getAppointMent(){
        return ipAddress + appointment;
    }

    public static String getAppointList(){
        return ipAddress + appointList;
    }


    /**
     * 获取ip地址
     * 连wifi传wifi地址，若无，则传移动网络地址
     */
    public static String getNetIp(Context context){
        ConnectivityManager conMann = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobileNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        String ip = "请连接网络";
        if(wifiNetworkInfo.isConnected()) {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddress = wifiInfo.getIpAddress();
            Log.d("wifiIpInt", String.valueOf(ipAddress));
            ip = transWifiIp(ipAddress);
            Log.d("wifiIp", ip);
        }else if (mobileNetworkInfo.isConnected()) {
            ip = getLocalIpAddress();
            Log.d("mobileIp", ip);
        }
        return ip;
    }

    public static String transWifiIp(int ipInt) {
        return (ipInt >> 24 & 0xFF) + "." +
                (ipInt >> 16 & 0xFF) + "." +
                (ipInt >> 8 & 0xFF) + "." +
                (ipInt & 0xFF);
    }

    public static String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface netInter = en.nextElement();
                for (Enumeration<InetAddress> enumIp = netInter.getInetAddresses(); enumIp.hasMoreElements();) {
                    InetAddress inetAddress = enumIp.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("localIp", ex.toString());
        }
        return null;
    }
}
